﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using AutoLoL.Logic.Automation;
using AutoLoL.Shared;

namespace AutoLoL.Dialogs
{
    /// <summary>
    /// Interaction logic for OverlayDialog.xaml
    /// </summary>
    public partial class OverlayTimersDialog : Window
    {
        private const double BASE_HEIGHT = 73;
        private const double ITEM_HEIGHT = 24;
        private const double ITEM_SPACING = 3;

        private DispatcherTimer _timer;
        private ObservableCollection<TimerInfo> _timers;
        private bool _isExpanded = false;

        public OverlayTimersDialog()
        {
            InitializeComponent();

            _timers = new ObservableCollection<TimerInfo>();

            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 5);
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.IsEnabled = true;

            _timers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Timers_CollectionChanged);

            TimersItemsControl.ItemsSource = _timers;

            ExpandButton_Click(null, null);
        }

        private void UpdateHeight()
        {
            if (_isExpanded)
            {
                this.Height = BASE_HEIGHT + (_timers.Count * ITEM_HEIGHT) + (Math.Max(0, _timers.Count) * ITEM_SPACING);
            }
        }

        private void Timers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateHeight();
        }

        private void ExpandButton_Click(object sender, RoutedEventArgs e)
        {
            CollapseButton.Visibility = System.Windows.Visibility.Visible;
            ExpandButton.Visibility = System.Windows.Visibility.Collapsed;
            this.Width = 212;
            _isExpanded = true;

            UpdateHeight();
        }

        private void CollapseButton_Click(object sender, RoutedEventArgs e)
        {
            CollapseButton.Visibility = System.Windows.Visibility.Collapsed;
            ExpandButton.Visibility = System.Windows.Visibility.Visible;
            this.Width = 82;
            this.Height = 32;
            _isExpanded = false;
        }

        private void DragButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            Win32.ReleaseCapture();
            Win32.SendMessage(handle, Win32.WM_NCLBUTTONDOWN, Win32.HT_CAPTION, 0);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            foreach (TimerInfo timer in _timers)
            {
                timer.Invalidate();
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void IconButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;

            if (b != null)
            {
                bool isValid = true;

                TimerInfo timer = new TimerInfo();
                timer.StartDate = DateTime.Now;

                switch (b.Tag.ToString())
                {
                    case "Baron":
                        timer.Duration = new TimeSpan(0, 0, 420);
                        timer.Icon = GetIcon("Baron.png");
                        break;
                    case "Dragon":
                        timer.Duration = new TimeSpan(0, 0, 360);
                        timer.Icon = GetIcon("Dragon.png");
                        break;
                    case "Golem":
                        timer.Duration = new TimeSpan(0, 0, 300);
                        timer.Icon = GetIcon("Golem.png");
                        break;
                    case "Lizard":
                        timer.Duration = new TimeSpan(0, 0, 300);
                        timer.Icon = GetIcon("Lizard.png");
                        break;
                    case "Ward":
                        timer.Duration = new TimeSpan(0, 0, 180);
                        timer.Icon = GetIcon("Ward.gif");
                        break;
                    default:
                        isValid = false;
                        break;
                }

                if (isValid)
                {
                    _timers.Add(timer);
                }
            }
        }

        private BitmapImage GetIcon(string file)
        {
            BitmapImage icon = new BitmapImage();
            icon.BeginInit();
            icon.UriSource = new Uri("pack://siteoforigin:,,,/Data/Images/Icons/" + file);
            icon.EndInit();
            return icon;
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image i = sender as Image;

            if (i != null)
            {
                TimerInfo t = i.Tag as TimerInfo;

                if (t != null)
                {
                    _timers.Remove(t);
                }
            }
        }

        private class TimerInfo : INotifyPropertyChanged
        {
            public DateTime StartDate { get; set; }

            public TimeSpan Duration { get; set; }

            public BitmapImage Icon { get; set; }

            public double Progress
            {
                get
                {
                    double progress = (DateTime.Now - StartDate).TotalSeconds;
                    return (progress / Duration.TotalSeconds) * 100.0;
                }
            }

            public void Invalidate()
            {
                OnPropertyChanged("Progress");
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}
